package com.neuedu.redis.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.neuedu.redis.config.CommonResult;
import com.neuedu.redis.entity.UmsUser;
import com.neuedu.redis.service.IUmsUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 用户账户表 前端控制器
 * </p>
 *
 * @author 金山
 * @since 2021-06-03
 */
@Slf4j
@RestController
@RequestMapping("/umsUser")
public class UmsUserController {

    @Resource
    IUmsUserService umsUserService;

    /**
     * 1 分页查询
     * http://127.0.0.1:8080/umsUser/list
     * @param page
     * @return
     */
    @GetMapping("/list")
    public CommonResult<Page<UmsUser>> list(Page page){
        Page result = umsUserService.page(page);

        log.info("当前页号:{}",page.getCurrent());
        log.info("总页数:{}",page.getPages());
        log.info("总条数:{}",page.getTotal());

        return CommonResult.success(result);
    }


    /**
     * http://127.0.0.1:8080/umsUser/token?username=root&password=123456
     * @param user
     * @return
     */
    @GetMapping("/token")
    public CommonResult<String> token(UmsUser user) throws JsonProcessingException {
        
        String token = umsUserService.token(user);
        if(StringUtils.isEmpty(token)){
            return CommonResult.failed("用户名或密码不正确");
        }
        return CommonResult.success(token);
    }






}
